<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */

namespace Admin\Controller;


class LeaveController extends AdminController
{
    const MODEL_NAME = 'Leave@:';

    //接口列表
    public function index()
    {
        if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['reason'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('createtime')&&I('endDate')){
            $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }            
        $map['status'] = array('neq','-1');
        $model = D('core_leave'); //存入数据库中，进行排序并显示页面
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $leave = M('core_leave_type')->where('id = '.$value['type'])->getField('title');
            $list[$key]['lname'] = $leave;
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '你好';
        $this->display(self::MODEL_NAME.'Leave/index'); 
        
    }
    public function addrecord()
    {
        if(IS_POST){
            $dd['leave_date']=strtotime(I('createtime'));
            $dd['create_time']=time();
            $dd['days']=I('days');
            $dd['reason']=I('reason');
            $dd['type']=I('type');
            $d=M('core_leave')->add($dd);
            $this->success('添加成功',U('index'));
        }else{
            $typeList = M('core_leave_type')->where('status='.'1')->select();
            $this->assign('typeList',$typeList);
            $this->meta_title = '新增接口';
            $this->display(self::MODEL_NAME.'Leave/addrecord');   
        }

    }
    public function editimpl()
    {
        if(IS_POST){
            $d['leave_date']=strtotime(I('createtime'));
            $d['reason']=I('reason');
            $d['days']=I('days');
            $d['type']=I('type');
            $d['update_time']=time();
            $f=M('core_leave')->where('id='.I('id'))->save($d);
            $this->success('修改成功',U('index'));
        }else{
            $ddd=M('core_leave')->where('id='.I('id'))->find();
            $this->assign('ddd',$ddd);
            $typeList = M('core_leave_type')->where('status = 1')->select();
            $this->assign('typeList',$typeList);
            $this->meta_title = '新增接口';
            $this->display(self::MODEL_NAME.'Leave/editimpl');
        }
    }

    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $model_object->check_time=time();
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $model_object->check_time="0";
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!',U('index'));
                }else{
                    $this->error('删除失败!',U('index'));
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
    public function type()
    {
        if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['reason'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('createtime')&&I('endDate')){
            $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }            
        $map['status'] = array('neq','-1');
        $model = D('core_leave_type'); //存入数据库中，进行排序并显示页面
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '你好';
        $this->display(self::MODEL_NAME.'Leave/type'); 
         
    }
    public function addtype()
    {
       
    if(IS_POST){
        $df['title']=I('title');
        $df['sort']=I('sort');
        $df['status']="1";
        $df['create_time']=time();
        $data = M('core_leave_type')->add($df);
        $this->success('添加成功',U('type'));
        
    }else{
        $this->display(self::MODEL_NAME.'Leave/addtype');
    }
    }
    public function edittype()
    {
       if(IS_POST){
        $f['title']=I('title');
        $f['update_time']=time();
        $s=M('core_leave_type')->where('id='.I('tid'))->save($f);
        $this->success('修改成功',U('type'));
       }else{
        $gg=M('core_leave_type')->where('id='.I('tid'))->find();
        $this->assign('gg',$gg);
        $this->display(self::MODEL_NAME.'Leave/edittype');
       }
    }
    public function leavestatist()
    {
       
        if(I('createtime')&&I('endDate')){
            switch (I('d_type')) {
                case '1':
                    $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
                    break;
                case '2':
                    $map['check_time'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
                    break;
                default:
                    $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
                    break;
            }
        }
        if(I('type')){
            $type=I('type');
            $map['type']=array('like','%'.$type.'%');
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }            
        $map['status'] = array('neq',-1);
        $model = D('core_leave'); //存入数据库中，进行排序并显示页面
        //总人数
        $list = $model->where($map)->select();
        $total = count($list);
        //已审核
        $succ = 0;
        //未审核
        $erro = 0;
        foreach ($list as $key => $value) {
            if($value['status'] == 1){
                $succ ++;
            }
            if($value['status'] == 2){
                $erro ++;
            }
        }
        $this->meta_title = '请假统计';
        $this->assign('total',$total);
        $this->assign('succ',$succ);
        $this->assign('erro',$erro);
        $this->display(self::MODEL_NAME.'Leave/leavestatist'); 
    }

} 
